﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace LightingServer
{
    public static class ResourceProvider
    {
        private static readonly Res _res;

        public static Res Res
        {
            get
            {
                return _res;
            }
        }

        static ResourceProvider()
        {
            Res.Builder builder = new Res.Builder();
            string resSql = "SELECT guid, itemID, itemName FROM res";
            using (DbConnection conn = LightingServerContext.CreateConnection())
            {
                using (DbCommand command = conn.CreateCommand())
                {
                    conn.Open();
                    command.CommandText = resSql;
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ResItem.Builder itemBuilder = new ResItem.Builder();
                            itemBuilder.SetId(uint.Parse(reader["guid"].ToString()));
                            itemBuilder.SetItemID(uint.Parse(reader["itemID"].ToString()));
                            itemBuilder.SetItemName(reader["itemName"].ToString());
                            builder.Res_List.Add(itemBuilder.Build());
                        }
                    }
                }
            }
            _res = builder.Build();
        }

        private static ResItem[] _productTypes;
        public static ResItem[] ProductTypes
        {
            get
            {
                if (_productTypes == null)
                {
                    _productTypes = GetResItems(1).ToArray();
                }
                return _productTypes;
            }
        }

        private static ResItem[] _useArea;
        public static ResItem[] UseArea
        {
            get
            {
                if (_useArea == null)
                {
                    _useArea = GetResItems(2).ToArray();
                }
                return _useArea;
            }
        }


        private static ResItem[] _orgin;
        public static ResItem[] Orgin
        {
            get
            {
                if (_orgin == null)
                {
                    _orgin = GetResItems(3).ToArray();
                }
                return _orgin;
            }
        }

        private static ResItem[] _defendType;
        public static ResItem[] DefendType
        {
            get
            {
                if (_defendType == null)
                {
                    _defendType = GetResItems(4).ToArray();
                }
                return _defendType;
            }
        }

        private static ResItem[] _lightingExplain;
        public static ResItem[] LightingExplain
        {
            get
            {
                if (_lightingExplain == null)
                {
                    _lightingExplain = GetResItems(5).ToArray();
                }
                return _lightingExplain;
            }
        }

        private static ResItem[] _workVoltage;
        public static ResItem[] WorkVoltage
        {
            get
            {
                if (_workVoltage == null)
                {
                    _workVoltage = GetResItems(6).ToArray();
                }
                return _workVoltage;
            }
        }

        private static ResItem[] _controlType;
        public static ResItem[] ControlType
        {
            get
            {
                if (_controlType == null)
                {
                    _controlType = GetResItems(7).ToArray();
                }
                return _controlType;
            }
        }

        private static ResItem[] _lightingType;
        public static ResItem[] LightingType
        {
            get
            {
                if (_lightingType == null)
                {
                    _lightingType = GetResItems(8).ToArray();
                }
                return _lightingType;
            }
        }

        private static ResItem[] _setupType;
        public static ResItem[] SetupType
        {
            get
            {
                if (_setupType == null)
                {
                    _setupType = GetResItems(9).ToArray();
                }
                return _setupType;
            }
        }

        public static IEnumerable<ResItem> GetResItems(uint guid)
        {
            return from r in _res.Res_List
                   where r.Id == guid
                   orderby r.ItemID
                   select r;
        }

        public static ResItem GetResItem(uint guid, string itemName)
        {
            return (from r in _res.Res_List
                    where r.Id == guid && r.ItemName == itemName
                    select r).FirstOrDefault();
        }
    }
}
